package com.streambolics.data;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.Inheritance;
import javax.jdo.annotations.InheritanceStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;

import com.streambolics.std.CalendarExtensions;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
@Inheritance(strategy = InheritanceStrategy.SUBCLASS_TABLE)
public class Agent extends Definition
{
    @Persistent
    private Double latitude;

    @Persistent
    private Double longitude;

    @Persistent
    private String address;

    @Persistent
    private String email;

    @Persistent
    private String timeZoneName;

    /**
     * Create a calendar with the appropriate time zone.
     * 
     * @return A newly instanciated Calendar, localized with the local timezone
     *         of the agent.
     */
    public Calendar createCalendar ()
    {
        return Calendar.getInstance (getTimeZone ());
    }

    public String dateToString (Date aDate)
    {
        Calendar c = createCalendar ();
        c.setTime (aDate);
        return CalendarExtensions.format (c, "DD/MM/YYYY HH:NN");
    }

    public String getAddress ()
    {
        return address;
    }

    public String getEmail ()
    {
        return email;
    }

    public double getLatitude ()
    {
        if (latitude == null)
        {
            return 0;
        }
        else
        {
            return latitude;
        }
    }

    public double getLongitude ()
    {
        if (longitude == null)
        {
            return 0;
        }
        else
        {
            return longitude;
        }
    }

    /**
     * The local timezone for the agent.
     * 
     * @return
     */

    public TimeZone getTimeZone ()
    {
        return TimeZone.getTimeZone (getTimeZoneName ());
    }

    /**
     * The name of the time zone used by the agent.
     * 
     * @return
     */

    public String getTimeZoneName ()
    {
        if (timeZoneName == null || timeZoneName.isEmpty ())
        {
            return "UTC";
        }
        else
        {
            return timeZoneName;
        }
    }

    public void setAddress (String aAddress)
    {
        this.address = aAddress;
    }

    public void setEmail (String aEmail)
    {
        email = aEmail;
    }

    public void setLatitude (Double aLatitude)
    {
        this.latitude = aLatitude;
    }

    public void setLongitude (Double aLongitude)
    {
        this.longitude = aLongitude;
    }

    public void setTimeZoneName (String aTimeZone)
    {
        timeZoneName = aTimeZone;
    }
}
